/* Data3.do (STATA)
	Construct holdings data.
	by Ralph Koijen & Motohiro Yogo */

#delimit ;
clear all;
set more off;
set type double;

cap log close;
log using Data3, replace;

/* Define local and global variables */

local directory = "../../Data/IMF/CPIS";

do global;


/* Step 1: Restate IMF CPIS data by nationality */

/* Load data */

u IMF_CPIS if country!="USA" & !Iofc, clear;	/* Exclude offshore financial centers */

drop Iofc;

/* Merge restatement matrices for bilateral positions */

joinby year country Counterpart type using Restatement_bilateral, unmatched(master);

/* Assume nationality is same as residency if unmatched (including fund shares) */

replace counterpart = Counterpart if _merge==1;
replace Value = 1 if _merge==1;

drop _merge;

/* Merge country code */

merge m:1 counterpart using Countries,
	keepusing(Yeuro Ynat)
	nogen keep(master match);

/* Restate from residency to nationality */

replace amount = amount*Value;

drop Value;

/* Assume currency is same as nationality as starting point */

gen currency = counterpart;
replace currency = "EUR" if year>=Yeuro;

/* Merge IMF CPIS currency composition */

merge m:1 year country type currency using IMF_CPIS_currency,
	nogen keep(master match);

/* Scale down to upper bound in domestic currency */

egen Tcounterpart = total(amount), missing by(year country type currency);

expand 2 if Tcurrency<Tcounterpart, gen(duplicate);

replace amount = amount*Tcurrency/Tcounterpart if Tcurrency<Tcounterpart & !duplicate;

/* Remainder in other currencies */

replace currency = "_OC" if Tcurrency<Tcounterpart & duplicate;

replace amount = amount*(1-Tcurrency/Tcounterpart) if Tcurrency<Tcounterpart & duplicate;

drop Tcurrency Tcounterpart duplicate;

/* Construct dummy for domestic currency */

gen byte Idomestic = currency==counterpart | (currency=="EUR" & year>=Yeuro);

drop currency Yeuro;

/* Assign countries outside sample to other countries */

replace counterpart = "_OC" if year<Ynat;

drop Ynat;

/* Aggregate by nationality */

collapse (sum) amount, by(year country counterpart type Idomestic) fast;

/* Save data */

tempfile IMF_CPIS;
save `IMF_CPIS';


/* Step 2: Restate US Treasury data by nationality */

/* Load data */

u Treasury, clear;

/* Merge restatement matrices for bilateral positions */

joinby year country Counterpart type using Restatement_bilateral, unmatched(master);

/* Assume nationality is same as residency if unmatched (including fund shares) */

replace counterpart = Counterpart if _merge==1;
replace Value = 1 if _merge==1;

drop _merge;

/* Merge country code */

merge m:1 counterpart using Countries,
	keepusing(Yeuro Ynat)
	nogen keep(master match);

/* Restate from residency to nationality */

replace amount = amount*Value;

drop Value;

/* Construct dummy for domestic currency */

gen byte Idomestic = currency==counterpart | (currency=="EUR" & year>=Yeuro) | type>=3;

drop currency Yeuro;

/* Assign countries outside sample to other countries */

replace counterpart = "_OC" if year<Ynat;

drop Ynat;

/* Aggregate by nationality */

collapse (sum) amount, by(year country counterpart type Idomestic) fast;

/* Append IMF CPIS data */

append using `IMF_CPIS';


/* Step 3: Construct own holdings */

/* Append amount outstanding */

append using Data2, gen(append);

replace country = counterpart if append;

drop append;

/* Aggregate amount outstanding */

collapse (sum) amount outstand, by(year country counterpart type Idomestic) fast;

/* Construct own holdings */

egen Tamount = total(amount*(country!=counterpart)), missing by(year counterpart type Idomestic);

replace amount = max(amount,outstand-Tamount,$small*Idomestic) if country==counterpart;

drop outstand Tamount;

/* Drop fund shares */

drop if type==4;

/* Aggregate foreign currency assets with outside assets */

replace counterpart = "_OC" if !Idomestic;

collapse (sum) amount, by(year country counterpart type) fast;


/* Step 4: Aggregate investor countries */

/* Merge country code */

merge m:1 country using Countries,
	keepusing(Ynat)
	nogen keep(master match);

/* Assign countries outside sample to other countries */

replace country = "_OC" if year<Ynat & country!="_CR";

drop Ynat;

/* Aggregate across other countries */

collapse (sum) amount, by(year country counterpart type) fast;

/* Round up to minimum value for CPIS */

replace amount = max(amount,1e-6) if counterpart!="_OC";

/* Winsorize left tail of outside assets */

egen wealthA = total(amount), missing by(year country type);

replace amount = max(amount,1e-3*wealthA) if counterpart=="_OC";

drop wealthA;

/* Label variables */

order year country counterpart type;

label var amount "Investment amount (US$ billion)";
 
/* Save data */

sort year country counterpart type;

save Data3, replace;

log close;
